<template>
  <!-- 危急值 -->
  <div id="criticalValue">
    <Breadcrumb content="危急值" />
    <div class="bodyContent">
      <vxe-toolbar perfect>
        <template #buttons>
          <vxe-form ref="criticalValueForm" title-align="right" title-width="60" :data="form">
            <vxe-form-gather span="24">
              <vxe-form-item title="检验日期" field="scheduleDate">
                <template #default="{ data }">
                  <vxe-input v-model="data.scheduleDateStart" placeholder="开始日期" type="date" transfer @change="findWtList"></vxe-input>
                  至
                  <vxe-input v-model="data.scheduleDateEnd" placeholder="结束日期" type="date" transfer @change="findWtList"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="上报时间" field="noticeDate">
                <template #default="{ data }">
                  <vxe-input v-model="data.noticeDateStart" placeholder="开始日期" type="datetime" transfer @change="findWtList"></vxe-input>
                  至
                  <vxe-input v-model="data.noticeDateEnd" placeholder="结束日期" type="datetime" transfer @change="findWtList"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="报告单元" field="rptGroup">
                <template #default>
                  <SelectWorkGroupAll :url="'/api/rptGroup/findRptGroupList?'+'businessType=20'" @fatherMethod="getSelectedId" :isShowAll="true" :isClear="true"></SelectWorkGroupAll>
                </template>
              </vxe-form-item>
            </vxe-form-gather>

            <vxe-form-gather span="24">
              <vxe-form-item title="病人类型" field="patType">
                <template #default="{ data }">
                  <vxe-select v-model="data.patType" placeholder="请选择病人类型" clearable class-name="width-100-percentage">
                    <vxe-option v-for="(item, index) in patTypeList" :key="index" :label="item.name" :value="item.code"></vxe-option>
                  </vxe-select>
                </template>
              </vxe-form-item>
              <vxe-form-item title="科室" field="depId">
                <template #default="{ data }">
                  <vxe-select v-model="data.reqDept" placeholder="科室" clearable class-name="width-100-percentage">
                    <vxe-option v-for="(item, index) in deptOptions" :key="index" :label="item.name" :value="item.id"></vxe-option>
                  </vxe-select>
                </template>
              </vxe-form-item>
              <vxe-form-item title="病历号" field="patNo">
                <template #default="{ data }">
                  <vxe-input v-model="data.patNo" clearable @clear="findWtList" @keyup.enter.native="findWtList"></vxe-input>
                </template>
              </vxe-form-item>
              <vxe-form-item title="患者姓名" field="patNa">
                <template #default="{ data }">
                  <vxe-input v-model="data.patNa" clearable @clear="findWtList" @keyup.enter.native="findWtList"></vxe-input>
                </template>
              </vxe-form-item>

              <vxe-form-item>
                <template #default>
                  <vxe-button type="text" class="button-blue" content="查询" @click="findWtList"></vxe-button>
                  <vxe-button type="text" class="button-blue" content="导出表格" @click="htmlToExcel"></vxe-button>
                  <vxe-button type="text" class="button-red" content="删除" @click="handleDelete"></vxe-button>
                </template>
              </vxe-form-item>
            </vxe-form-gather>
          </vxe-form>
        </template>
      </vxe-toolbar>

      <div class="tableBody">
        <vxe-table ref="criticalValueTable" v-loading="loading" border height="100%" :data="criticalValueList" align="left" :row-config="{ isCurrent: true, isHover: true }" resizable show-overflow @current-change="handleRowClick">
          <vxe-column type="seq" title="#" width="30"></vxe-column>
          <vxe-column field="reportTime" title="上报时间" width="120">
            <template #default="{ row }">
              <span>{{ row.reportTime }}</span>
            </template>
          </vxe-column>
          <vxe-column field="rptGroupName" title="报告单元" width="120">
            <template #default="{ row }">
              <span>{{ row.rptGroupName }}</span>
            </template>
          </vxe-column>
          <vxe-column field="sampleda" title="检验日期" width="80">
            <template #default="{ row }">
              <span>{{ formatDateTime(row.sampleda, 'yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </vxe-column>
          <vxe-column field="sampleNo" title="标本号" width="60">
            <template #default="{ row }">
              <span>{{ row.sampleNo }}</span>
            </template>
          </vxe-column>
          <vxe-column field="reqNo" title="条码号" width="120">
            <template #default="{ row }">
              <span>{{ row.reqNo }}</span>
            </template>
          </vxe-column>
          <vxe-column field="patNo" title="病历号" width="120">
            <template #default="{ row }">
              <span>{{ row.patNo }}</span>
            </template>
          </vxe-column>
          <vxe-column field="patNa" title="病人姓名" width="80">
            <template #default="{ row }">
              <span>{{ row.patNa }}</span>
            </template>
          </vxe-column>
          <vxe-column field="reqDept" title="科室" width="120">
            <template #default="{ row }">
              <span>{{ row.reqDept }}</span>
            </template>
          </vxe-column>
          <vxe-column field="bedNo" title="床号" width="40">
            <template #default="{ row }">
              <span>{{ row.bedNo }}</span>
            </template>
          </vxe-column>
          <vxe-column field="sampleTypeName" title="样本类型" width="80">
            <template #default="{ row }">
              <span>{{ row.sampleTypeName }}</span>
            </template>
          </vxe-column>
          <vxe-column field="culticalItemType" title="流程节点" width="120">
            <template #default="{ row }">
              <span>{{ getNodeName(row.criticalItemType) }}</span>
            </template>
          </vxe-column>
          <vxe-column field="currentResult" title="结果" width="80"></vxe-column>
          <vxe-column field="reportPerson" title="上报人" width="80">
            <template #default="{ row }">
              <span>{{ row.reportPerson }}</span>
            </template>
          </vxe-column>
          <vxe-column field="notifyPerson" title="接收人" width="80">
            <template #default="{ row }">
              <span>{{ row.notifyPerson }}</span>
            </template>
          </vxe-column>
          <vxe-column field="requestNo" title="申请单号" width="120"></vxe-column>
          <vxe-column field="remark" title="备注"></vxe-column>
          <vxe-column field="measure" title="检验科处理措施" width="120">
            <template #default="{ row }">
              <span>{{ row.measure }}</span>
            </template>
          </vxe-column>
          <!-- <vxe-column field="createTime" title="上报时间" width="80">
            <template #default="{ row }">
              <span>{{ formatDateTime(row.createTime, 'yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </vxe-column> -->
          <vxe-column field="reqTime" title="申请时间" width="80">
            <template #default="{ row }">
              <span>{{ formatDateTime(row.reqTime, 'yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </vxe-column>
          <vxe-column field="testTime" title="上机时间" width="80">
            <template #default="{ row }">
              <span>{{ formatDateTime(row.testTime, 'yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </vxe-column>
          <vxe-column field="chkTime" title="审核时间" width="80">
            <template #default="{ row }">
              <span>{{ formatDateTime(row.chkTime, 'yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </vxe-column>
          <vxe-column field="reviewerTime" title="医生确认时间" width="80">
            <template #default="{ row }">
              <span>{{ formatDateTime(row.reviewerTime, 'yyyy-MM-dd hh:mm:ss') }}</span>
            </template>
          </vxe-column>
          <vxe-column field="notifyPerson" title="医生确认人" width="80">
            <template #default="{ row }">
              <span>{{ row.notifyPerson }}</span>
            </template>
          </vxe-column>
          <vxe-column field="treatmentMeasure" title="医生处理意见" width="80">
            <template #default="{ row }">
              <span>{{ row.treatmentMeasure }}</span>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
    </div>
  </div>
</template>
<script lang="ts" setup>
import Breadcrumb from '@/components/Breadcrumb.vue';
import { reactive, ref, defineExpose, getCurrentInstance, onMounted } from 'vue';
import { IsEmpty, sortByField, formatDateTime } from '@/utils/common';
import { VxeMessage, VxeNotifyType } from "@/utils/CommonMessage";
import SelectWorkGroupAll from '../lab_bus/components/SelectWorkGroupAll.vue'; //报告单元默认全部引入下拉组件
import { AppStore } from '@/stores/store';

let store = AppStore();
let { proxy }: any = getCurrentInstance();
let loading = ref(false);
let criticalValueList = reactive([]);
let patTypeList = reactive([]);//字典:病人类型

let form = reactive({
  /*  orgId: store.userInfo.orgId,
    orgName: store.userInfo.orgName,*/
  orgId: '',
  orgName: '',
  scheduleDateStart: formatDateTime(new Date(), 'yyyy-MM-dd'),
  scheduleDateEnd: formatDateTime(new Date(), 'yyyy-MM-dd'),
  noticeDateStart: null,
  noticeDateEnd: null,
  rptGroup: null,
  itemNo: null,
  patType: '',
  reqDept: '',
  patNo: '',
  patNa: ''
});
let formData = reactive({});
let deptOptions: any = reactive([]); //选择框
const orgList = store.userInfo.orgList;
let defaultProps = {
  children: 'children',
  label: 'name'
};

// 页面加载时字典
onMounted(() => {
  getDepyOptions(); //获取科室
  getDictPatTypeList();//病人类型
});

function getNodeName(culticalItemType){
  if (culticalItemType == '1'){
    return '仪器培养(血培养)';
  }
  if (culticalItemType == '2'){
    return '培养观察';
  }
  if (culticalItemType == '3'){
    return '细菌结果';
  }
  if (culticalItemType == '4'){
    return '药敏结果';
  }
  if (culticalItemType == '5'){
    return '染色';
  }
  return '';
}

//行点击时间
function handleRowClick({ row }) {
  Object.assign(formData, row);
  proxy.$refs.criticalValueTable.setCurrentRow(row);
}

function htmlToExcel(title = '危急值表格') {
  (proxy.$refs.criticalValueTable as any).exportData({ type: 'csv', filename: title, useStyle: true });

}

// 点击删除
async function handleDelete() {


  if (IsEmpty(formData) || IsEmpty(formData.id)) {
    VxeMessage('请勾选要删除的数据', VxeNotifyType.warning);
    return;
  }
  let data: any = {
    id: formData.id
  };
  proxy.$confirm('确定删除此数据？', '提示', {
    cancelButtonClass: 'cancel-button',
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      proxy.$http
        .get('/lab/criticalReport/deleteCriticalReportInfo', { params: data })
        .then((res: any) => {
          if (res.status.code === 1) {
            VxeMessage('删除成功', VxeNotifyType.success);
            findWtList();
          } else {
            VxeMessage('删除失败', VxeNotifyType.error);
          }
        })
        .catch((err: any) => {
          VxeMessage('删除异常', VxeNotifyType.error);
        });
    });

}

function handleOrgNodeClick(data: any) {
  form.orgId = data.id;
  form.orgName = data.name;
  findWtList();
  const selectOrgRef: any = proxy.$refs['selectOrgRef'];
  selectOrgRef.blur();
}
// 获取所有部门选项(下拉框)
function getDepyOptions() {
  const url = '/api/sys/findDepartmentList';
  proxy.$http
    .get(url, { params: { orgId: store.userInfo.orgId } })
    .then((res: any) => {
      deptOptions.push(...res.data.deptList);
    })
    .catch((err: any) => {
      VxeMessage('获取所有部门选项(下拉框)失败', VxeNotifyType.error);
    });
}
function getDictPatTypeList() {
  patTypeList.length = 0;
  const url = '/api/sys/basDictDetail/findDictDetailList';
  proxy.$http
    .get(url, { params: { code: 'PATTYPE' } })
    .then((res: any) => {
      patTypeList.push(...res.data);
    })
    .catch((err: any) => {
      proxy.$alert(err, '提示');
    });
}

function findWtList() {
  criticalValueList.length = 0;
  const url = '/lab/criticalReport/findBacCriticalReportInfo';

  form.scheduleDateStart = IsEmpty(form.scheduleDateStart) ? '' : formatDateTime(form.scheduleDateStart, 'yyyy-MM-dd') + ' 00:00:00';
  form.scheduleDateEnd = IsEmpty(form.scheduleDateEnd) ? '' : formatDateTime(form.scheduleDateEnd, 'yyyy-MM-dd') + ' 23:59:59';
  proxy.$http
    .post(url, form)
    .then((res: any) => {
      criticalValueList.length = 0;
      proxy.$nextTick(() => {
        criticalValueList.push(...res.data);
        handleRowClick({ row: res.data[0] });
      })

      loading.value = false;
    })
    .catch(err => {
      loading.value = false;
      VxeMessage(err, VxeNotifyType.error);
    });
}

// 报告单元
// 下拉报告单元获取报告单元id
function getSelectedId(selectedRadioId, selectedName) {
  form.rptGroup = selectedRadioId.value;
  findWtList();
}
// 组件
//点击选择的那一项受检单位
function SelectRowClick(row, index) {
  form.itemNo = row.itemNo;
}

</script>
<style lang="scss" scoped>
#criticalValue {
  height: 100%;

  .bodyContent {
    height: calc(100% - 60px);

    .vxe-form {
      padding: 5px 10px;

      :deep(.vxe-form--item) {
        margin: 0 10px 0 0;
      }
    }

    .tableBody {
      background-color: $base-white;
      height: calc(100% - 76px);
    }
  }

  // 附加按钮间距
  .button-blue,
  .button-red {
    margin: 0 5px 0 0;
  }

  // 表格最左侧5px间距
  :deep(.vxe-cell) {
    padding-left: 5px;
  }

  :deep(.vxe-form) {

    .vxe-input.type--datetime,
    .vxe-input.type--date {
      width: 150px;
    }

    .el-date-editor {
      height: 24px;
    }

    .el-range-editor.el-input__inner {
      padding: 0;
      width: 320px;
      border-radius: 0;
    }
  }
}
</style>
